package com.example.web.sys_menu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.web.sys_menu.entity.MakeMenuTree;
import com.example.web.sys_menu.entity.Menu;
import com.example.web.sys_menu.mapper.MenuMapper;
import com.example.web.sys_menu.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LS
 * @since 2024-02-20
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> getParent(){
        String[] type={"0","1"};
        List<String> strings= Arrays.asList(type);
        QueryWrapper<Menu> query=new QueryWrapper<>();
        query.lambda().in(Menu::getType,strings).orderByAsc(Menu::getOrderNum);
        List<Menu> menuList=this.baseMapper.selectList(query);
        //组装顶级树
        Menu menu = new Menu();
        menu.setTitle("顶级菜单");
        menu.setLabel("顶级菜单");
        menu.setParentId(-1L);
        menu.setMenuId(0L);
        menu.setValue(0L);
        menuList.add(menu);
        //组装菜单树
        return MakeMenuTree.makeTree(menuList,-1L);
    }

    @Override
    public  List<Menu> getMenuByUserId(Long userId){
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public  List<Menu> getMenuByRoleId(Long roleId){
        return this.baseMapper.getMenuByRoleId(roleId);
    }

}
